package com.carrie.config.handler;

import com.alibaba.fastjson2.JSON;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;

import java.io.IOException;
import java.util.HashMap;

/**
 * @author XuHaiLong
 * @since 2025/4/15 14:22
 */
public class MyAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request, HttpServletResponse response, AccessDeniedException accessDeniedException) throws IOException, ServletException {
        HashMap<String,Object> map = new HashMap<>();
        map.put("code",403);
        map.put("msg","没有权限");
        response.setContentType("application/json;charset=utf-8");//响应头
        response.getWriter().println(JSON.toJSONString(map));//写入一个响应结果给前端
    }
}
